package com.itheima.config;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

import javax.servlet.Filter;

/*public class ServletConfig extends AbstractDispatcherServletInitializer {
    @Override
    //加载spring配置类。产生springmvc容器。本质还是spring容器
    protected WebApplicationContext createServletApplicationContext() {
        //初始化webapplicationContext对象
        AnnotationConfigWebApplicationContext app = new AnnotationConfigWebApplicationContext();
        app.register(SpringMvcConfig.class);
        return app;
    }

    @Override
    //设置有springmvc控制器处理的请求映射路径
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    @Override
    //加载spring配置类,其他资源配置，不参与映射，只参与资源使用
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
}*/
//方法二，简写模式
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    //加载配置类。不参与映射
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    @Override
    //请求映射路径
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    @Override
    //拦截所有请求
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //中文乱码处理
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("utf-8");
        return new Filter[]{filter};
    }
}